<script>
    // 判断浏览器是否支持 Service Worker
    if ('serviceWorker' in navigator) {
        // 在 load 事件触发后注册 Service Worker，确保 Service Worker 的注册不会影响首屏速度
        window.addEventListener('load', function () {
            navigator.serviceWorker.getRegistrations()
                .then(regs => {
                    for(let reg of regs){
                        reg.unregister()
                    }
                    // 注册 Service Worker
                    navigator.serviceWorker.register('/sw.js').then(registration => {
                        // 注册成功
                        console.log('ServiceWorker registration successful with scope: ', registration.scope)
                    }).catch(err => { 
                        // 注册失败 :(
                        console.warn('ServiceWorker registration failed: ', err)
                    })
                })
        })
    }
</script>